package app.netant.book.sc70;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class Sc70Detail extends BaseDetail {

	public Sc70Detail(String netSite) {
		super(netSite);
		this.preHref = "http://www.70sc.net";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select("#m #m_c #c #right .infopage .title h1");
		if(parts.size()>0){
			String title = parts.get(0).text();
			int idx = title.indexOf("作者");
			if(idx>0){
				title = title.substring(0,idx).trim();
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title+".txt");
		}
	}

	@Override
	public void analyseTags() {
		Elements parts = this.select("#m #m_c #c #right .infopage .tab table tbody tr td");
		for(int i=0;i<parts.size();i++){
			String tags = this.getString(parts.get(i).text());
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim().replaceAll(" ", "");
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				this.entity.setTag(tagName, tagValue);
			}
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select("#m #m_c #c #right .infopage .info");
		if(parts.size()>0){
			String memo = parts.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select("#m #m_c #c #right .infopage .todo ul li a.blue");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			if(text.indexOf("下载")>=0){
				String refer = parts.get(i).attr("href");
				if(refer.startsWith("/")){
					refer = this.preHref + refer;
				}
				System.out.println("【Refer】:"+refer);
				this.entity.setRefer(refer);
				
				Sc70Detail detail = new Sc70Detail(refer);
				String href = detail.stepHref();
				System.out.println("【Href】:"+href);
				this.entity.setDownPath(href);
				
				break;
			}
		}
	}
	
	public String stepHref(){
		Elements parts = this.select("#m #m_c .main .main-z .downlink a");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			if(text.indexOf("直接下载")>0){
				return parts.get(i).attr("href");
			}
		}
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select("#m #m_c #c #right .infopage .position a");
		if(parts.size()>1){
			String type = parts.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		Sc70Detail detail = new Sc70Detail("http://www.70sc.net/txt/89226.html");
		detail.test();
	}



}
